package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.louwowo.cache.key.RedisKeys;
import cn.wolfcode.louwowo.cache.service.IScenicStatisRedisService;
import cn.wolfcode.luowowo.article.domain.Scenicspot;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicspotService;
import cn.wolfcode.luowowo.comment.domain.ScenicspotComment;
import cn.wolfcode.luowowo.comment.domain.ScenicspotReply;
import cn.wolfcode.luowowo.comment.query.ScenicspotCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicspotCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IScenicspotTemplateSearchService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("scenic")
public class ScenicSpotController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicspotService scenicspotService;
    @Reference
    private IScenicspotTemplateSearchService scenicspotTemplateSearchService;
    @Reference
    private IScenicspotCommentService scenicspotCommentService;
    @Reference
    private IScenicStatisRedisService scenicStatisRedisService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("destId") Long destId) {
        model.addAttribute("destSurvey",destinationService.getSurveyById(destId));
        model.addAttribute("top5Scenic",scenicspotService.getTop5());
        model.addAttribute("hotScenic",scenicspotService.getHotScenic());
        List<String> catalogs = scenicspotService.getCatalogs();
        model.addAttribute("catalogs",scenicspotService.getCatalogs());
        return "scenicSpot/list";
    }

    @RequestMapping("catalog")
    public String catalog(Model model,@ModelAttribute("qo") SearchQueryObject qo){
        model.addAttribute("page",scenicspotTemplateSearchService.list(qo));
        return "scenicSpot/listTpl";
    }

    @RequestMapping("detail")
    public String detail(Model model,Long id,@UserParam UserInfo userInfo){
        Scenicspot scenicspot = scenicspotService.get(id);
        if(scenicspot.getImageList() != null){
            model.addAttribute("imageSize",scenicspot.getImageList().size() + 1);
        }
        model.addAttribute("scenicspot",scenicspot);
        model.addAttribute("hasImg",scenicspotCommentService.getStatisNum().get("hasImg"));
        model.addAttribute("goodComment",scenicspotCommentService.getStatisNum().get("goodComment"));
        model.addAttribute("middleComment",scenicspotCommentService.getStatisNum().get("middleComment"));
        model.addAttribute("badComment",scenicspotCommentService.getStatisNum().get("badComment"));
        model.addAttribute("landmark",scenicspotCommentService.getStatisNum().get("landmark"));
        model.addAttribute("manyPeople",scenicspotCommentService.getStatisNum().get("manyPeople"));
        model.addAttribute("worth",scenicspotCommentService.getStatisNum().get("worth"));
        model.addAttribute("fearHigh",scenicspotCommentService.getStatisNum().get("fearHigh"));
        model.addAttribute("easyTraffic",scenicspotCommentService.getStatisNum().get("easyTraffic"));
        model.addAttribute("fineView",scenicspotCommentService.getStatisNum().get("fineView"));
        if(userInfo != null){
            //用户登陆时，判断该攻略是否被当前用户收藏，用于收藏样式的回显
            model.addAttribute("isFavor",scenicStatisRedisService.isFavor(userInfo.getId(),id));
            model.addAttribute("hasBeen",scenicStatisRedisService.hasBeen(userInfo.getId(),id));
        }
        return "scenicSpot/detail";
    }

    @RequestMapping("content")
    public String content(Model model,Long id){
        model.addAttribute("scenic",scenicspotService.get(id));
        return "scenicSpot/detailTpl";
    }

    @RequestMapping("comment")
    public String comment(Model model, ScenicspotComment comment,
                          String image1, String image2, String image3,
                          @UserParam UserInfo userInfo,
                          ScenicspotReply reply, ScenicspotCommentQuery qo){
        if(userInfo != null && reply != null){
            //补充用户信息
            comment.setUserId(userInfo.getId());
            comment.setUsername(userInfo.getNickname());
            comment.setLevel(userInfo.getLevel());
            comment.setHeadUrl(userInfo.getHeadImgUrl());
            reply.setCurrentUserHeadImg(userInfo.getHeadImgUrl());
            reply.setCurrentUserId(userInfo.getId());
            reply.setCurrentUserName(userInfo.getNickname());
            //处理评论的图片
            List<String> imgList = new ArrayList<>();
            if(image1 != null){
                imgList.add(image1);
            }
            if(image2 != null){
                imgList.add(image2);
            }
            if(image3 != null){
                imgList.add(image3);
            }
            String images = String.join(",",imgList);
            comment.setImages(images);
            ScenicspotComment comm = scenicspotCommentService.saveOrUpdate(comment,reply);
        }
        model.addAttribute("page",scenicspotCommentService.query(qo));
        return "scenicSpot/commentTpl";
    }

    @RequestMapping("tag")
    public String tag(Model model,ScenicspotCommentQuery qo){
        Page query = scenicspotCommentService.query(qo);
        model.addAttribute("page",scenicspotCommentService.query(qo));
        return "scenicSpot/commentTpl";
    }

    @RequestMapping("image")
    public String image(Model model,Long id){
        model.addAttribute("scenic",scenicspotService.get(id));
        return "scenicSpot/image";
    }

    @RequestMapping("favor")
    @ResponseBody
    public AjaxResult favor(Long sid,@UserParam UserInfo userInfo){
        //先判断是否登陆
        if(userInfo == null){
            return AjaxResult.UNLOGIN();
        }
        boolean b = scenicStatisRedisService.favor(userInfo.getId(),sid);
        //true表示是收藏操作，false表示取消收藏
        return new AjaxResult(b,"").getData(scenicStatisRedisService.getStatisVo(sid));
    }

    @RequestMapping("been")
    @ResponseBody
    public AjaxResult been(Long sid,@UserParam UserInfo userInfo){
        //先判断是否登陆
        if(userInfo == null){
            return AjaxResult.UNLOGIN();
        }
        boolean b = scenicStatisRedisService.been(userInfo.getId(),sid);
        //true表示是收藏操作，false表示取消收藏
        return new AjaxResult(b,"").getData(scenicStatisRedisService.getStatisVo(sid));
    }

    @RequestMapping("commentThumbUp")
    @ResponseBody
    public AjaxResult commentThumbUp(String commentId,@UserParam UserInfo userInfo){
        //先判断是否登陆
        if(userInfo == null){
            return AjaxResult.UNLOGIN();
        }
        boolean b = scenicStatisRedisService.thumbup(commentId, userInfo.getId());
        int num = 0;
        if(!b){
            //加赞数操作
            num = scenicspotCommentService.addThumbNum(commentId, 1);
        }
        //true表示是已顶过了，false表示今天还没顶
        return new AjaxResult(!b,"").getData(num);
    }

}
